/**
 *
 * @type 公共方法
 */

const os = require('os');
const log = require('./log');
const RoomData = require("../room/room-data");

let baseTool = {
    xhpx_key: 'ZShL88DzjqXhpAi8',
    verifyPhone(mob) {
        return /^(\+86)*0*1[0-9]\d{9}$/.test(mob);
    },
    verifyCode(code) {
        return /^\d{6}/.test(code);
    },
    verifyName(name) {
        return /^[A-Za-z]{1}\w{3,20}$/.test(name);
    },
    verifyPwd(pwd) {
        return /^[A-Za-z]{1}\w{3,20}$/.test(pwd);
    },

    // 删除指定字符串
    delString(str, val) {
        let reg = new RegExp(val)
        return str.replace(reg, '')
    },
    // 删除左右两端的空格
    trim(str) {
        return str.replace(/(^\s*)|(\s*$)/g, "");
    },
    // 生成10位时间戳
    time() {
        return Math.round(new Date().getTime() / 1000).toString();
    },
    // 判断用户是否登录
    isLogin(req) {
        const uid = req.body.uid;
        const xbtk = req.cookies.get('xbtk');
        if (!uid) {
            log.msg(`uid：${uid}`)
            return false;
        }
        if (!xbtk) {
            log.msg(`xbtk：${xbtk}`)
            return false;
        }
        return {uid, xbtk};
    },
    // 指定范围的随机数
    random(minNum,maxNum){
        switch(arguments.length){
            case 1:
                return parseInt(Math.random()*minNum+1,10);
                break;
            case 2:
                return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10);
                break;
            default:
                return 0;
                break;
        }
    },
    // 生成订单号
    createOrderNumber(uid = 1) {
        // 时间戳
        const time = this.time();
        // 服务器gid + uid
        const gid = os.userInfo().gid + os.userInfo().uid;
        const count = parseInt(time)
        // 获取2-8之间的随机数
        const min = 10000;
        const mxa = 99999;
        const Range = mxa - min;
        const Rand = Math.random();
        const num = min + Math.floor(Rand * Range);  //舍去
        // console.log(num);
        // return;
        // 初步11位日期：年月日时分秒
        const d = new Date();
        const year = d.getFullYear();

        let month = d.getMonth() + 1;
        if (month.toString().length<2) month = `0${month}`;

        let day = d.getDate();
        if (day.toString().length<2) day = `0${day}`;

        let hour = d.getHours();
        if (hour.toString().length<2) hour = `0${hour}`;


        let minute = d.getMinutes();
        if (minute.toString().length<2) minute = `0${minute}`;

        let second = d.getSeconds();
        if (second.toString().length<2) second = `0${second}`;

        const date = year + month + day + hour + minute + second;

        let orderNumber = (count * num + uid.toString()).toString();
        // 订单号小于9位，开头补0
        if (orderNumber.length < 9) {
            orderNumber = this._PrefixInteger(orderNumber, 9);
        }
        // 订单号大于9位，从后取9位数字
        if (orderNumber.length > 9) {
            orderNumber = orderNumber.substr(orderNumber.length - 9);
        }

        // 生成最终订单号
        return 'XB' + date.toString() + orderNumber;
    },
    // 用0补位
    _PrefixInteger(num, length) {
        return (Array(length).join('0') + num).slice(-length);
    }
}

module.exports = baseTool;